package com.bestv.db;

import com.bestv.util.DbConstant;
import com.bestv.util.GlobalSettings;

/**
 * Created by charle-chen on 15/11/18.
 */
public class DbFactory {

    private static GlobalSettings globalSettings=null;
    private static String driver = "";
    private static String url = "";
    private static String username = "";
    private static String password = "";

    private static void init(int type){
        globalSettings = GlobalSettings.getInstance();

        switch (type){
            case DbConstant.MYSQL_DB:
                driver = globalSettings.getMysqlDbDriver();
                url = globalSettings.getMysqlDbUrl();
                username = globalSettings.getMysqlUsername();
                password = globalSettings.getMysqlPassword();
                break;
            case DbConstant.ORACLE_DB:
                driver = globalSettings.getOracleDbDriver();
                url = globalSettings.getOracleDbUrl();
                username = globalSettings.getOracleUsername();
                password = globalSettings.getOralcePassword();
                break;
            default:
                break;
        }
    }

    public static Db getDbInstance(int type){
        Db db=null;
        init(type);

        switch (type){
            case DbConstant.MYSQL_DB:
                db = new MysqlDb(driver,url,username,password);
                break;
            case DbConstant.ORACLE_DB:
                db = new OracleDb(driver,url,username,password);
                break;
            default:
                break;
        }
        return db;
    }
}
